//
// Created by xinhua on 2021/10/22.
//

#include "Color.h"


Color::Color() {
    this->r = 0;
    this->g = 0;
    this->b = 0;
    this->a = 1;
}

Color::Color(float r, float g, float b, float a) {
    this->r = r;
    this->g = g;
    this->b = b;
    this->a = a;
}

Color::Color(float v[4]) {
    if(MAssert::isNull(v)){
        return;
    }
    r = v[0]; g = v[1]; b = v[2];  a = v[3];
}


Color::Color(const Color &v) {
    r = v.r; g = v.g; b = v.b; a = v.a;
}

Color &Color::operator=(const Color &v) {
    r = v.r;
    g = v.g;
    b = v.b;
    a = v.a;
    return *this;
}

bool Color::operator==(const Color &v) const{
    return r == v.r && g == v.g && b == v.b && a == v.a;
}

bool Color::operator!=(const Color &v) const{
    return r != v.r || g != v.g || b != v.b || a != v.a;
}

Color Color::operator+(const Color &v) const{
    return Color(r + v.r, g + v.g, b + v.b, a + v.a);
}

Color Color::operator-(const Color &v) const{
    return Color(r - v.r, g - v.g, b - v.b, a - v.a);
}

Color Color::operator*(float v) const{
    return Color(r * v, g * v, b * v, a * v);
}

Color Color::operator/(float v) const{
    float _a = 1 / v;
    return Color(r * _a, g * _a, b * _a, a * _a);
}

float Color::length() {
    return std::sqrt(r * r + g * g + b * b + a * a);
}

void Color::normalize() {
    float len = length();
    r = r / len;
    g = g / len;
    b = b / len;
    a = a / len;
}

float Color::operator*(const Color &v) const{
    return r * v.r + g * v.g + b * v.b + a * v.a;
}

String Color::toString() {
    String str = "(r = " + std::to_string(r) + ", g = " + std::to_string(g) + ", b = " + std::to_string(b) + ", a = " + std::to_string(a) + ")";
    return str;
}

/**
* 向量相减
*/
Color Color::subtractVectors(const Color& v1, const Color& v2) {
    float _r = v1.r - v2.r;
    float _g = v1.g - v2.g;
    float _b = v1.b - v2.b;
    float _a = v1.a - v2.a;
    return Color(_r, _g, _b, _a);
}

/**
 * 标量左乘
 * @param f 标量
 * @param v2 向量
 * @return 结果
 */
Color Color::multiply(float f, Color v2) {
    return Color(v2.r * f, v2.g * f, v2.b * f, v2.a * f);
}

